<?php 
namespace Admin\Model;
use Think\Model;

class NormsModel extends Model
{
	/**
	 * [norms_sele 通过当前商品id搜索数据库查找相对应的信息]
	 * @return [type] [返回当前商品的信息]
	 */
	public function norms_sele()
	{
		$gid = I('get.id');
		$map['gid'] = ['eq',$gid];
		$list['norms'] = $this->where($map)->find();
		$list['param'] = $this->table('zx_param')->where($map)->find();
		return $list;
	}

	/**
	 * [norms_add 添加或修改当前商品的参数]
	 * @return [type] [修改成功与否均返回]
	 */
	public function norms_add()
	{
		$norms = I('post.');
		$norms['size'] = I('post.norsize');

		$reg = '/^[1-9][0-9]{0,}$/';
		if(!preg_match($reg, $norms['size'])){
			return "over";
		}
		$map['gid'] = ['eq',$norms['gid']];		
		$list = $this->where($map)->find();

		if($list['gid']){
			$resn = $this->where($map)->save($norms);
		}else{
			$res = $this->add($norms);
		}

		$param['gid'] = I('post.gid');
		$param['goodsname'] = I('post.goodsname');
		$param['weight'] = trim(I('post.weight')) ;
		$param['size'] = trim(I('post.parsize'));
		$param['place'] = trim(I('post.place'));
		$param['equiv'] = trim(I('post.equiv'));
		$regp = '/[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u';

		if(empty($param['weight'])){
			return 'over';
		}

		if(empty($param['size'])){
			return 'over';
		}

		if(empty($param['place'])){
			return 'over';
		}

		if(empty($param['equiv'])){
			return 'over';
		}

		$respa = M('param');
		$list2 = $respa->where($map)->find();

		if($list2){					//判断表中是否存在当前商品的参数
			$res = $respa->where($map)->save($param);
			if($res){
				return 'update';
			}else{
				return 'non';
			}
		}else{
			$res = $respa->add($param);
			if($res){
				return 'update';
			}else{
				return 'non';
			}
		}
	}
}